//
//  YBAssetsDetailTableViewCell.m
//  Nectar
//
//  Created by famer on 2023/11/12.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBAssetsDetailTableViewCell.h"

@implementation YBAssetsDetailTableViewCell


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        
        self.contentView.backgroundColor = kThemeBackground;
        
        _bgView = [[UIView alloc]initWithFrame:CGRectZero];
        _bgView.clipsToBounds = YES;
        _bgView.layer.cornerRadius = 10;
        _bgView.backgroundColor = kWhiteColor;
        [self.contentView addSubview:_bgView];
        
        _typeImageView = [[UIImageView alloc] init];
        _typeImageView.contentMode = UIViewContentModeScaleAspectFit;
        _typeImageView.image = [UIImage imageNamed:@"icon_transfer_out"];
        [_bgView addSubview:_typeImageView];
        
        _addressLabel = [[UILabel alloc] init];
        _addressLabel.font = [UIFont systemFontOfSize:12];
        _addressLabel.text = @"*";
        _addressLabel.textColor = kThemeBlackColor;
        _addressLabel.textAlignment = NSTextAlignmentLeft;
        _addressLabel.numberOfLines = 0;
        _addressLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [_bgView addSubview:_addressLabel];
        
        _dateLabel = [[UILabel alloc] init];
        _dateLabel.text = @"*";
        _dateLabel.font = [UIFont systemFontOfSize:10];
        _dateLabel.textColor = [UIColor hexColor:@"84828C"];
        _dateLabel.textAlignment = NSTextAlignmentLeft;
        _dateLabel.numberOfLines = 0;
        _dateLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [_bgView addSubview:_dateLabel];
        
        _amountLabel = [[UILabel alloc] init];
        _amountLabel.text = @"*";
        _amountLabel.font = [UIFont systemFontOfSize:12];
        _amountLabel.textColor = [UIColor hexColor:@"FF575C"];
        _amountLabel.textAlignment = NSTextAlignmentLeft;
        _amountLabel.numberOfLines = 0;
        _amountLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [_bgView addSubview:_amountLabel];
        
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kThemeSplitLineColor;
        [_bgView addSubview:_lineView];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat width = self.contentView.size.width;
    CGFloat height = self.contentView.size.height;
    CGFloat leftMargin = 12;
    
    
    self.bgView.frame = CGRectMake(0, 0, width, height-0);
    
    self.typeImageView.frame = CGRectMake(leftMargin,
                                          0,
                                          16,
                                          16);
    
    self.typeImageView.centerY = self.bgView.height*0.5;
    
    CGSize addressLabelSize = [self.addressLabel textBoundingSizeWithMaxSize:CGSizeMake(width, 500)];
    self.addressLabel.frame = CGRectMake(self.typeImageView.right+14,
                                         0,
                                         addressLabelSize.width,
                                         addressLabelSize.height);
    
    self.addressLabel.centerY = self.typeImageView.centerY-addressLabelSize.height*0.5-3;
    
    CGSize dateLabelSize = [self.dateLabel textBoundingSizeWithMaxSize:CGSizeMake(width, 500)];
    self.dateLabel.frame = CGRectMake(self.addressLabel.left,
                                      0,
                                      dateLabelSize.width,
                                      dateLabelSize.height);
    
    self.dateLabel.centerY = self.typeImageView.centerY+addressLabelSize.height*0.5+3;
    
    
    ///
    CGSize amountLabelSize = [self.amountLabel textBoundingSizeWithMaxSize:CGSizeMake(width, 500)];
    self.amountLabel.frame = CGRectMake(self.bgView.width-leftMargin-amountLabelSize.width,
                                        0,
                                        amountLabelSize.width,
                                        amountLabelSize.height);
    
    self.amountLabel.centerY = self.typeImageView.centerY;
    
    self.lineView.frame = CGRectMake(leftMargin, self.bgView.height-1, self.bgView.width-leftMargin*2, 1);
}

- (void)setData:(RevDataGetMyTransactions *)item{
    if(item){
        self.addressLabel.text = item.address;
        self.dateLabel.text = item.createTime;

        NSDecimalNumber *amountBit = [NSDecimalNumber decimalNumberWithString:item.amount];
        self.amountLabel.text = [NSString stringWithFormat:@"%@ USDT", [amountBit stringValue]];
        if([[item.inOrOut lowercaseString] containsString:@"out"]){
            self.amountLabel.textColor = [UIColor hexColor:@"FF575C"];
            self.amountLabel.text = [NSString stringWithFormat:@"-%@ USDT", [amountBit stringValue]];
            self.typeImageView.image = [UIImage imageNamed:@"icon_transfer_out"];
            
        } else  if([[item.inOrOut lowercaseString] containsString:@"in"]){
            self.amountLabel.textColor = kThemeColor;
            self.amountLabel.text = [NSString stringWithFormat:@"+%@ USDT", [amountBit stringValue]];
            self.typeImageView.image = [UIImage imageNamed:@"icon_transfer_in"];
        }
        
        [self setNeedsLayout];
        [self layoutIfNeeded];
    }
}

@end
